package com.wanxi.education.controller.back;

import com.github.pagehelper.PageInfo;
import com.wanxi.education.common.CommonResult;
import com.wanxi.education.model.vo.advertandcertificate.AdvertAddVo;
import com.wanxi.education.model.vo.advertandcertificate.AdvertFindPageVo;
import com.wanxi.education.service.AdvertService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @Author: yanguangqiong
 * @Description:
 * @Date: Created in 2021/9/24
 * @Version: 1.0
 */
@Api(tags = "后台管理员广告管理")
@RestController
@RequestMapping("/admin/back/advert")
public class AdvertBackController {
    @Autowired
    private AdvertService advertService;

    @PostMapping("/findPage")
    @ApiOperation("后台管理员查看广告列表")
    public CommonResult<PageInfo<AdvertAddVo>> findPage(@RequestBody AdvertFindPageVo advertFindPageVo) {
        if (ObjectUtils.isEmpty(advertFindPageVo.getPageNum())) {
            return CommonResult.failure("第几页为空");
        }
        if (ObjectUtils.isEmpty(advertFindPageVo.getPageSize())) {
            return CommonResult.failure("每页几条为空");
        }
        PageInfo pageInfo = advertService.findPage(advertFindPageVo);
        return CommonResult.success(pageInfo);
    }

    @GetMapping("/findById")
    @ApiOperation("后台管理员查看广告详情、修改的回显")
    public CommonResult<AdvertAddVo> findById(@RequestParam("advertId") Long advertId) {
        if (ObjectUtils.isEmpty(advertId)) {
            return CommonResult.failure("广告id为空");
        }
        AdvertAddVo advertAddVo = advertService.findById(advertId);
        return CommonResult.success(advertAddVo);
    }

    @PostMapping("/add")
    @ApiOperation("后台管理员增加广告信息")
    public CommonResult add(@RequestBody AdvertAddVo advertAddVo, HttpSession httpSession) {
        if (StringUtils.isBlank(advertAddVo.getImgSrc())) {
            return CommonResult.failure("主图为空");
        }
        if (StringUtils.isBlank(advertAddVo.getUrl())) {
            return CommonResult.failure("网址链接为空");
        }
        Long operatorId = (Long) httpSession.getAttribute("LoginId");
        advertAddVo.setOperatorId(operatorId);
        advertService.add(advertAddVo);
        return CommonResult.success("success");
    }

    @PostMapping("/update")
    @ApiOperation("后台管理员修改广告信息")
    public CommonResult update(@RequestBody AdvertAddVo advertAddVo,HttpSession httpSession) {
        if (StringUtils.isBlank(advertAddVo.getImgSrc())) {
            return CommonResult.failure("主图为空");
        }
        if (StringUtils.isBlank(advertAddVo.getUrl())) {
            return CommonResult.failure("网址链接为空");
        }
        if (ObjectUtils.isEmpty(advertAddVo.getId())) {
            return CommonResult.failure("广告id为空");
        }
        Long operatorId = (Long) httpSession.getAttribute("LoginId");
        advertAddVo.setOperatorId(operatorId);
       advertService.update(advertAddVo);
        return CommonResult.success("success");
    }

    @PostMapping("/delete")
    @ApiOperation("后台管理员删除广告信息")
    public CommonResult delete(@RequestParam("advertId") Long advertId,HttpSession httpSession) {
        if (ObjectUtils.isEmpty(advertId)) {
            return CommonResult.failure("广告id为空");
        }
        advertService.delete(advertId);
        return CommonResult.success("success");
    }
}
